<template>
  <div class="login-account">
    <el-form
      ref="formRef"
      label-width="65px"
      :rules="rules"
      :model="account"
      status-icon
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElForm } from 'element-plus'

import { useLoginStore } from '@/store'
import { rules } from '../config/rules-config'
import localCache from '@/utils/cache'

const loginStore = useLoginStore()

const account = reactive({
  name: localCache.getCache('name') ?? '',
  password: localCache.getCache('password') ?? ''
})

const formRef = ref<InstanceType<typeof ElForm>>()

const loginAction = (isKeepPassword: boolean) => {
  formRef.value?.validate((valid) => {
    if (valid) {
      // 1.判断是否需要记住密码
      if (isKeepPassword) {
        localCache.setCache('name', account.name)
        localCache.setCache('password', account.password)
      } else {
        localCache.deleteCache('name')
        localCache.deleteCache('password')
      }
      // 2.开始进行登录验证
      loginStore.accountLoginAction({ ...account })
    }
  })
}

defineExpose({
  loginAction
})
</script>

<style scoped lang="less"></style>
